package com.iap.common.service
{
	import com.iap.common.event.SampleEvent;
	import com.iap.app.data.AppData;
	import com.iap.app.data.IAppData;
	import com.iap.app.model.ServiceResponse;
	import com.iap.common.model.Sample;
	import com.iap.common.signal.SampleRequestSignal;
	import com.iap.common.translator.SampleTranslator;
	
	import flash.net.URLRequestMethod;
	import flash.utils.getQualifiedClassName;
	
	import org.robotlegs.mvcs.Actor;
	
	public class SampleService extends Actor implements ISampleService
	{
		[Inject]
		public var data:IAppData;

		private var _task:String;
		public function set task(value:String):void { _task = value; }

		public function getList():void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, AppData.JOB_GET);
		}
		
		public function save(model:Sample):void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, AppData.JOB_SAVE, model);
		}

		public function remove(model:Sample):void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, AppData.JOB_REMOVE, model);
		}

		public function saveFavorite(model:Sample):void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, SampleRequestSignal.SAVE_FAVORITE, model, URLRequestMethod.POST);
		}

		private function responseHandler(response:ServiceResponse):void
		{
			data.dataComplete.remove(responseHandler);
			response.task = _task;
			response.result = SampleTranslator.translate(response.result);
			dispatch(new SampleEvent(SampleEvent.RESPONSE, response));
		}
	}
}